//
//  KFDViewModel.m
//  KFD
//
//  Created by JIRENTIANXIANG on 6/10/15.
//  Copyright (c) 2015 kfd. All rights reserved.
//

#import "KFDViewModel.h"
#import <objc/runtime.h>

@implementation KFDViewModel

-(void)iterateAllProperties
{
    unsigned int numberOfProperties = 0;
    objc_property_t *propertyArray = class_copyPropertyList([self class], &numberOfProperties);
    
    for (NSUInteger i = 0; i < numberOfProperties; i++)
    {
        objc_property_t property = propertyArray[i];
        NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];
        NSString *attributesString = [[NSString alloc] initWithUTF8String:property_getAttributes(property)];
        NSLog(@"\n\n\n\nProperty %@ attributes: %@\n\n\n\n", name, attributesString);
    }
    free(propertyArray);
}

-(void)registerObserver:(id)observer
{
    unsigned int numberOfProperties=0;
    objc_property_t* propertyArray=class_copyPropertyList([self class], &numberOfProperties);
    for (NSUInteger i=0; i<numberOfProperties; i++) {
        objc_property_t property=propertyArray[i];
        NSString* name=[[NSString alloc] initWithUTF8String:property_getName(property)];
        [self addObserver:observer forKeyPath:name options:NSKeyValueObservingOptionNew context:nil];
    }
}

@end
